package com.ugoodtech.mdcc.client.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.StationTechColorReportService;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.domain.TechColorAuditingReports.TechColorAuditingReports;
import com.ugoodtech.mdcc.core.repository.*;
import io.swagger.annotations.ApiParam;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StationTechColorReportServiceImpl implements StationTechColorReportService {

    private Logger logger = LoggerFactory.getLogger(StationTechColorReportServiceImpl.class);

    @Autowired
    private StationRepository stationRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private StationReportRepository stationReportRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private StationColorReportRepository stationColorReportRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuditReportRepository auditReportRepository;

    @Autowired
    private AuditColorReportRepository auditColorReportRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private AuditReportRecorderRepository auditReportRecorderRepository;

    @Autowired
    private UserLogRepository userLogRepository;


    @Override
    public JsonResponse getAudidApply(String province, String city, String category, String brand, String customerStat, String customerType,
                                      String paintBrand, String productPosition, String department, String payService, String sentTime,
                                      String serviceTime, String stationName, Long startTime, Long endTime, String userName, String stationNum,
                                      Pageable pageable, User user,Integer status) {
        String[] cities = null;
        String[] categories = null;
        String[] departments = null;
        String[] customerStats = null;
        String[] customerTypes = null;
        String[] paintBrands = null;
        String[] productPositions = null;
        String[] provinces = null;
        String[] brands = null;

        if (!StringUtils.isEmpty(category)){
            categories = category.split(",");
        }
        if (StringUtils.isNotEmpty(city)){
            cities = city.split(",");
        }
        if (StringUtils.isNotEmpty(department)){
            departments = department.split(",");
        }
        if (!StringUtils.isEmpty(paintBrand)){
            paintBrands = paintBrand.split(",");
        }
        if (!StringUtils.isEmpty(productPosition)){
            productPositions = productPosition.split(",");
        }
        if (!StringUtils.isEmpty(customerStat)){
            customerStats = customerStat.split(",");
        }
        if (!StringUtils.isEmpty(customerType)){
            customerTypes = customerType.split(",");
        }
        if (!StringUtils.isEmpty(province)){
            provinces = province.split(",");
        }
        if (!StringUtils.isEmpty(brand)){
            brands = brand.split(",");
        }


        QAuditReport qAuditReport = QAuditReport.auditReport;
        QAuditColorReport qAuditColorReport = QAuditColorReport.auditColorReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qAuditReport.deleted.eq(false));
        builder.and(qAuditReport.enabled.eq(true));
        builder.and(qAuditReport.supplement.eq(false));
        builder.and(qAuditReport.category.eq(Constants.TECHCOLOR_REPORT));
        if (status != null) {
            if (status == 0) {
                builder.and(qAuditReport.auditing.eq(Constants.AUDIT_APPLY));
            } else {
                builder.and(qAuditReport.auditing.in(Constants.AUDIT_TRUE, Constants.AUDIT_FALSE));
            }
        }
        if (startTime != null && startTime != 0){
            builder.and(qAuditReport.updateTime.goe(new Date(startTime)));
        }
        if(endTime != null && endTime != 0) {
            builder.and(qAuditReport.updateTime.lt(new Date(endTime)));
        }
        List<Long> ids = new ArrayList<>();
        if (categories!=null) {BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(7L));
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str : categories) {
                builderOr.or(qAuditColorReport.answerOther.like("%" + str + "%"));
            }
            builder1.and(builderOr);
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (provinces!=null) {
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(173L));
            builder1.and(qAuditColorReport.answerOther.in(provinces));
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (brands!=null) {
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(8L));
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str : brands) {
                builderOr.or(qAuditColorReport.answerId.like("%" + str + "%"));
            }
            builder1.and(builderOr);
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (StringUtils.isNotEmpty(stationName)) {
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(175L));
            builder1.and(qAuditColorReport.answerOther.like("%" + stationName + "%"));
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (StringUtils.isNotEmpty(stationNum)){
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(181L));
            builder1.and(qAuditColorReport.answerOther.eq(stationName));
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (cities!=null) {
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(174L));
            builder1.and(qAuditColorReport.answerOther.in(cities));
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (customerStats != null) {
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(184L));
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str : customerStats) {
                builderOr.or(qAuditColorReport.answerId.like("%" + str + "%"));
            }
            builder1.and(builderOr);
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (customerTypes != null) {
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(185L));
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str : customerTypes) {
                builderOr.or(qAuditColorReport.answerId.like("%" + str + "%"));
            }
            builder1.and(builderOr);
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (paintBrands!=null) {
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(13L));
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str : paintBrands) {
                builderOr.or(qAuditColorReport.answerId.like("%" + str + "%"));
            }
            builder1.and(builderOr);
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (productPositions!=null) {
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            builder1.and(qAuditColorReport.questionId.eq(182L));
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str : productPositions) {
                builderOr.or(qAuditColorReport.answerId.like("%" + str + "%"));
            }
            builder1.and(builderOr);
            List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
            if (list == null || list.size() == 0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
            }
            for (AuditColorReport report:list){
                ids.add(report.getAuditReport().getId());
            }
        }
        if (payService != null || sentTime!=null || serviceTime!=null){
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qAuditColorReport.deleted.eq(false));
            builder1.and(qAuditColorReport.enabled.eq(true));
            boolean flag = false;
            if (payService!=null){
                flag = true;
                builder1.and(qAuditColorReport.questionId.eq(209L));
                builder1.and(qAuditColorReport.answerId.like("%"+payService+"%"));
            }
            if (sentTime!=null){
                flag = true;
                builder1.and(qAuditColorReport.questionId.eq(218L));
                builder1.and(qAuditColorReport.answerId.like("%"+sentTime+"%"));
            }
            if (serviceTime!=null){
                flag = true;
                builder1.and(qAuditColorReport.questionId.eq(219L));
                builder1.and(qAuditColorReport.answerId.like("%"+serviceTime+"%"));
            }
            if (flag) {
                List<AuditColorReport> list = (List<AuditColorReport>) auditColorReportRepository.findAll(builder1);
                if (list == null || list.size() == 0) {
                    return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
                }
                for (AuditColorReport report : list) {
                    ids.add(report.getAuditReport().getId());
                }
            }
        }
        if (ids != null && ids.size()>0) {
            builder.and(qAuditReport.id.in(ids));
        }
        if (StringUtils.isNotEmpty(userName) || departments != null) {
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            boolean flag = false;
            if (StringUtils.isNotEmpty(userName)) {
                flag = true;
                booleanBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            }
            if (departments != null){
                flag = true;
                booleanBuilder.and(qUserProfile.user.userFlag.in(0,2));
                booleanBuilder.and(qUserProfile.user.userType.in(UserType.admin,UserType.dealers,UserType.insiders));
                for (String str:departments){
                    booleanBuilder.or(qUserProfile.departmentList.any().name.eq(str));
                }
            }
            if (flag) {
                List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
                if (userProfileList == null || userProfileList.size() == 0) {
                    return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList(), pageable, 0));
                }
                List<Long> userIds = new ArrayList<>();
                for (UserProfile user1 : userProfileList) {
                    userIds.add(user1.getUser().getId());
                }
                builder.and(qAuditReport.user.id.in(userIds));
            }
        }

        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<AuditReport> auditReports = auditReportRepository.findAll(builder, pageable);
        List<TechColorAuditingReports> reports = new ArrayList<>();
        for (AuditReport report : auditReports.getContent()) {
            TechColorAuditingReports techColorAuditingReports = new TechColorAuditingReports();
            techColorAuditingReports.setId(report.getId());
            techColorAuditingReports.setUserType(report.getUserType());
            AuditColorReport colorReport = auditColorReportRepository.selectReport(report.getId(), 175L);
            AuditColorReport proinvce = auditColorReportRepository.selectReport(report.getId(), 173L);
            AuditColorReport city1 = auditColorReportRepository.selectReport(report.getId(), 174L);
            if (colorReport != null && proinvce != null && city1 != null) {
                String provinceStr = proinvce.getAnswerOther();
                String cityStr = city1.getAnswerOther();
                if (provinceStr.equals(cityStr)) {
                    techColorAuditingReports.setStationName(provinceStr + colorReport.getAnswerOther());
                } else {
                    techColorAuditingReports.setStationName(proinvce.getAnswerOther() + city1.getAnswerOther() + colorReport.getAnswerOther());
                }
            } else {
                techColorAuditingReports.setStationName("");
            }
            colorReport = auditColorReportRepository.selectReport(report.getId(), 7L);
            if (colorReport != null) {
                if (StringUtils.isEmpty(colorReport.getAnswerId())) {
                    techColorAuditingReports.setStationType("");
                } else {
                    QuestionAnswerOption answer = questionAnswerOptionRepository.findOne(Long.valueOf(colorReport.getAnswerId()));
                    if (answer == null) {
                        techColorAuditingReports.setStationType("");
                    } else {
                        techColorAuditingReports.setStationType(answer.getName());
                    }
                }
            } else {
                techColorAuditingReports.setStationType("");
            }

            techColorAuditingReports.setDateTime(report.getUpdateTimeStr());
            try {
                UserProfile userProfile = userProfileRepository.findByUserId(report.getUser().getId());
                techColorAuditingReports.setUserName(userProfile.getRealName());
            } catch (Exception e) {
                techColorAuditingReports.setUserName("默认的名字");
            }
            reports.add(techColorAuditingReports);
        }
        Page<TechColorAuditingReports> page = new PageImpl(reports, pageable, auditReports.getTotalElements());
        return JsonResponse.successResponseWithPageData(page);
    }

    @Override
    public JsonResponse auditTrue(User user, Long reportId,Integer userType) {
        logger.info("报告ID为:" + reportId);
        logger.info("开始进行审核");
        AuditReport auditReport = auditReportRepository.findOne(reportId);
        if (auditReport == null) {
            logger.info("报告不存在");
            return JsonResponse.errorResponseWithError("抱歉", "报告不存在");
        }
        List<AuditColorReport> colorReports = auditColorReportRepository.selectAnswer(reportId);
        for (AuditColorReport report : colorReports)
            if (report.getQuestionId() == 175L) {
                List<Station> stationNumList = stationRepository.findByStationNameList(report.getAnswerOther());
                if (!stationNumList.isEmpty()) {
                    if (auditReport.getStation() != null && !auditReport.getStation().getName().equals(report.getAnswerOther())) {
                        return JsonResponse.errorResponseWithError("抱歉", "维修站名称重复了");
                    }
                }
            } else if (report.getQuestionId() == 181L) {
                //验证维修站编号是否已经存在
                List<Station> stationNumList = stationRepository.findByStationNumList(report.getAnswerOther());
                if (!stationNumList.isEmpty()) {
                    if (auditReport.getStation() != null && !auditReport.getStation().getStationNum().equals(report.getAnswerOther())) {
                        return JsonResponse.errorResponseWithError("抱歉", "维修站编号重复了");
                    }
                }
            }
        logger.info("判断是否是重复提交的报告");
        if (auditReport.isReAudit() == true) {
            logger.info("是重复提交的报告");
            Station station = auditReport.getStation();
            if (station.getReport_finished()==0){
                station.setReport_finished(userType+1);
            }else if((station.getReport_finished() == 1 && userType == 1)||(station.getReport_finished() == 2 && userType == 0)){
                station.setReport_finished(3);
            }
            User reUser = null;
            UserProfile userProfile = new UserProfile();
            AuditColorReport departmentReport = null;
            for (AuditColorReport report : colorReports){
                if (report != null && report.getQuestionId() != null) {
                    switch (report.getQuestionId().intValue()){
                        case 175:
                            logger.info("查询维修站名称");
                            station.setName(report.getAnswerOther());
                            break;
                        case 173:
                            logger.info("查询维修站省份");
                            station.setProvince(report.getAnswerOther());
                            break;
                        case 174:
                            logger.info("查询维修站城市");
                            station.setCity(report.getAnswerOther());
                            break;
                        case 186:
                            logger.info("查询维修站地址");
                            station.setAddress(report.getAnswerOther());
                            break;
                        case 4:
                            reUser = userRepository.findByUsername(report.getAnswerOther());
                            if (reUser == null) {
                                reUser = new User();
                                reUser.setUserFlag(1);
                                reUser.setUsername(report.getAnswerOther());
                                reUser.setUserType(UserType.customer);
                                reUser.setPassword(passwordEncoder.encode(Constants.INIT_PASSWORD));
                            } else {
                                if (reUser.getUserFlag() != 1){
                                    reUser.setUserFlag(2);
                                }
                                if (null != reUser && reUser.getUserType().equals(UserType.insiders)) {
                                    return JsonResponse.errorResponseWithError("抱歉", "该人员是内部人员无法作为维修站联系人");
                                }
                                userProfile = userProfileRepository.findByUserId(reUser.getId());
                            }
                            logger.info("set User信息");
                            userRepository.save(reUser);
                            logger.info("保存User成功");
                            station.setUser(reUser);

                            logger.info("setUserProfile信息");
                            userProfile.setUser(reUser);
                            userProfile.setTelephone(report.getAnswerOther());
                            UserProfile managerUser = userProfileRepository.findByUserId(user.getId());
                            userProfile.setAddUser(managerUser);
                            break;
                        case 2:
                            logger.info("查询联系人姓名");
                            station.setContact(report.getAnswerOther());
                            userProfile.setRealName(report.getAnswerOther());
                            break;
                        case 3:
                            logger.info("查询联系人职位");
                            userProfile.setPosition(report.getAnswerOther());
                            break;
                        case 8:
                            logger.info("查询维修站品牌");
                            String[] brandIds = report.getAnswerId().split(",");
                            StringBuffer stringBuffer = new StringBuffer("");
                            for (String id : brandIds) {
                                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (id.equals("369")) {
                                    stringBuffer.append(report.getAnswerOther() + ",");
                                } else {
                                    if (answerOption.getName()!=null&& answerOption.getName()!=null) {
                                        stringBuffer.append(answerOption.getName() + ",");
                                    }
                                }
                            }
                            station.setBrand(stringBuffer.toString());
                            report.setAnswerOther(report.getAnswerOther());
                            auditColorReportRepository.save(report);
                            break;
                        case 184:
                            logger.info("查询客户状态");
                            if (StringUtils.isNotEmpty(report.getAnswerId())) {
                                station.setCustomerStat(Long.valueOf(report.getAnswerId()));
                            }
                            break;
                        case 185:
                            logger.info("查询客户类型");
                            if (StringUtils.isNotEmpty(report.getAnswerId())) {
                                station.setCustomerType(Long.valueOf(report.getAnswerId()));
                            }
                            break;
                        case 13:
                            logger.info("查询油漆品牌");
                            if (StringUtils.isNotEmpty(report.getAnswerId())) {
                                station.setPaintBrand(report.getAnswerId());
                            }
                            break;
                        case 182:
                            logger.info("查询品牌定位");
                            if (StringUtils.isNotEmpty(report.getAnswerId())) {
                                station.setProductPosition(report.getAnswerId());
                            }
                            break;
                        case 7:
                            logger.info("查询维修站类型");
                            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(report.getAnswerId()));
                            station.setCategory(answerOption.getName());
                            report.setAnswerOther(answerOption.getName());
                            auditColorReportRepository.save(report);
                            break;
                        case 181:
                            logger.info("查询维修站编号");
                            station.setStationNum(report.getAnswerOther());
                            break;
                        case 14:
                            departmentReport = report;
                            break;
                    }
                }
            }

            stationRepository.save(station);
            auditReport.setStation(station);
            auditReport.setAuditing(Constants.AUDIT_TRUE);
            auditReport.setReAudit(false);
            try {
                station.setOtherBrand(auditReport.isOtherBrnad());
                //先保存维修站
                station.setDeleted(false);
                station.setEnabled(true);
                station.setColorReportId(reportId);
                station.setCheckStation(true);
                stationRepository.save(station);
                logger.info("查询经销商");
                if (departmentReport != null && StringUtils.isNotEmpty(departmentReport.getAnswerOther())) {
                    Department department = departmentRepository.selectByName(departmentReport.getAnswerOther());
                    if (department != null) {
                        List<Station> stationList = department.getStationList();
                        if (stationList != null && stationList.size() > 0) {
                            stationList.add(station);
                        } else {
                            stationList = new ArrayList<>();
                            stationList.add(station);
                        }
                        department.setStationList(stationList);
                        departmentRepository.save(department);
                    }
                }
                //在保存用户
                userRepository.save(reUser);
                //再设置维修站的联系人
                station.setUser(reUser);
                station.setDeleted(false);
                station.setEnabled(true);
                stationRepository.save(station);
                //保存报告
                auditReportRepository.save(auditReport);
                //保存用户索关联的维修站ID
                reUser.setStationId(station.getId());
                userRepository.save(reUser);
                //最后将用户详情与用户关联
                userProfile.setUser(reUser);
                userProfileRepository.save(userProfile);

                AuditReportRecorder auditReportRecorde = new AuditReportRecorder();
                auditReportRecorde.setAuditReport(auditReport);
                auditReportRecorde.setTrue_false(true);
                auditReportRecorde.setUser(user);
                auditReportRecorderRepository.save(auditReportRecorde);

                return updateColorReport(auditReport.getId(), station,userType);
            } catch (Exception e) {
                e.printStackTrace();
                return JsonResponse.errorResponseWithError("抱歉", "审核失败，请重试");
            }
        }


        logger.info("报告存在");
        logger.info("开始新建维修站");

        Station station = new Station();
        UserProfile userProfile = new UserProfile();
        User user1 = null;
        AuditColorReport departmentReport = null;
        for (AuditColorReport report : colorReports){
            if (report != null && report.getQuestionId() != null) {
                switch (report.getQuestionId().intValue()){
                    case 175:
                        if (auditReport.getStation() != null) {
                            station = auditReport.getStation();
                        }
                        station.setReport_finished(userType+1);
                        logger.info("查询维修站名称");
                        station.setName(report.getAnswerOther());
                        break;
                    case 173:
                        logger.info("查询维修站省份");
                        station.setProvince(report.getAnswerOther());
                        break;
                    case 174:
                        logger.info("查询维修站城市");
                        station.setCity(report.getAnswerOther());
                        break;
                    case 186:
                        logger.info("查询维修站地址");
                        station.setAddress(report.getAnswerOther());
                        break;
                    case 4:
                        logger.info("开始新建UserProfile");
                        logger.info("查询联系人手机");
                        String phone = report.getAnswerOther();
                        user1 = userRepository.findByUsername(phone);
                        if (user1 == null) {
                            user1 = new User();
                            user1.setUserFlag(1);
                            user1.setUsername(report.getAnswerOther());
                            user1.setUserType(UserType.customer);
                            user1.setPassword(passwordEncoder.encode(Constants.INIT_PASSWORD));
                        } else {
                            if (user1.getUserFlag() != 1){
                                user1.setUserFlag(2);
                            }
                            if (null != user1 && user1.getUserType().equals(UserType.insiders)) {
                                return JsonResponse.errorResponseWithError("抱歉", "该人员是内部人员无法作为维修站联系人");
                            }
                            userProfile = userProfileRepository.findByUserId(user1.getId());
                        }
                        logger.info("set User信息");
                        userRepository.save(user1);
                        logger.info("保存User成功");
                        station.setUser(user1);

                        logger.info("setUserProfile信息");
                        userProfile.setUser(user1);
                        userProfile.setTelephone(report.getAnswerOther());
                        UserProfile managerUser = userProfileRepository.findByUserId(user.getId());
                        userProfile.setAddUser(managerUser);
                        break;
                    case 2:
                        logger.info("查询联系人姓名");
                        station.setContact(report.getAnswerOther());
                        userProfile.setRealName(report.getAnswerOther());
                        break;
                    case 3:
                        logger.info("查询联系人职位");
                        userProfile.setPosition(report.getAnswerOther());
                        break;
                    case 8:
                        logger.info("查询维修站品牌");
                        String[] brandIds = report.getAnswerId().split(",");
                        StringBuffer stringBuffer = new StringBuffer("");
                        for (String id : brandIds) {
                            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (id.equals("369")) {
                                stringBuffer.append(report.getAnswerOther() + ",");
                            } else {
                                if (answerOption.getName()!=null&& answerOption.getName()!=null) {
                                    stringBuffer.append(answerOption.getName() + ",");
                                }
                            }
                        }
                        station.setBrand(stringBuffer.toString());
                        report.setAnswerOther(report.getAnswerOther());
                        auditColorReportRepository.save(report);
                        break;
                    case 184:
                        logger.info("查询客户状态");
                        if (StringUtils.isNotEmpty(report.getAnswerId())) {
                            station.setCustomerStat(Long.valueOf(report.getAnswerId()));
                        }
                        break;
                    case 185:
                        logger.info("查询客户类型");
                        if (StringUtils.isNotEmpty(report.getAnswerId())) {
                            station.setCustomerType(Long.valueOf(report.getAnswerId()));
                        }
                        break;
                    case 13:
                        logger.info("查询油漆品牌");
                        if (StringUtils.isNotEmpty(report.getAnswerId())) {
                            station.setPaintBrand(report.getAnswerId());
                        }
                        break;
                    case 182:
                        logger.info("查询品牌定位");
                        if (StringUtils.isNotEmpty(report.getAnswerId())) {
                            station.setProductPosition(report.getAnswerId());
                        }
                        break;
                    case 7:
                        logger.info("查询维修站类型");
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(report.getAnswerId()));
                        station.setCategory(answerOption.getName());
                        report.setAnswerOther(answerOption.getName());
                        auditColorReportRepository.save(report);
                        break;
                    case 181:
                        logger.info("查询维修站编号");
                        station.setStationNum(report.getAnswerOther());
                        break;
                    case 14:
                        departmentReport = report;
                        break;
                }
            }
        }

        stationRepository.save(station);
        auditReport.setStation(station);
        auditReport.setAuditing(Constants.AUDIT_TRUE);
        try {
            //先保存维修站
            station.setOtherBrand(auditReport.isOtherBrnad());
            station.setDeleted(false);
            station.setEnabled(true);
            station.setColorReportId(reportId);
            station.setCheckStation(true);
            stationRepository.save(station);
            logger.info("查询经销商");
            if (departmentReport != null && StringUtils.isNotEmpty(departmentReport.getAnswerOther())) {
                Department department = departmentRepository.selectByName(departmentReport.getAnswerOther());
                if (department != null) {
                    List<Station> stationList = department.getStationList();
                    if (stationList != null && stationList.size() > 0) {
                        stationList.add(station);
                    } else {
                        stationList = new ArrayList<>();
                        stationList.add(station);
                    }
                    department.setStationList(stationList);
                    departmentRepository.save(department);
                }
            }
            //在保存用户
            userRepository.save(user1);
            //再设置维修站的联系人
            station.setUser(user1);
            station.setDeleted(false);
            station.setEnabled(true);
            stationRepository.save(station);
            //保存报告
            auditReportRepository.save(auditReport);
            //保存用户索关联的维修站ID
            user1.setStationId(station.getId());
            userRepository.save(user1);
            //最后将用户详情与用户关联
            userProfile.setUser(user1);
            userProfileRepository.save(userProfile);

            AuditReportRecorder auditReportRecorde = new AuditReportRecorder();
            auditReportRecorde.setAuditReport(auditReport);
            auditReportRecorde.setTrue_false(true);
            auditReportRecorde.setUser(user);
            auditReportRecorderRepository.save(auditReportRecorde);

            return updateColorReport(auditReport.getId(), station,userType);
        } catch (Exception e) {
            return JsonResponse.errorResponseWithError("抱歉", "审核失败，请重试");
        }
    }

    private JsonResponse updateColorReport(Long auditReportId, Station station,Integer userType) {
        AuditReport auditReport = auditReportRepository.findOne(auditReportId);
        QAuditColorReport qAuditColorReport = QAuditColorReport.auditColorReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qAuditColorReport.deleted.eq(false));
        builder.and(qAuditColorReport.enabled.eq(true));
        builder.and(qAuditColorReport.auditReport.id.eq(auditReportId));
        List<AuditColorReport> auditColorReportList = (List<AuditColorReport>) auditColorReportRepository.findAll(builder);
        QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
        builder = new BooleanBuilder();
        builder.and(qStationColorReport.deleted.eq(false));
        builder.and(qStationColorReport.enabled.eq(true));
        builder.and(qStationColorReport.stationReport.station.id.eq(auditReport.getStation().getId()));
        List<StationColorReport> stationColorReportList = (List<StationColorReport>) stationColorReportRepository.findAll(builder);
        if (stationColorReportList == null || stationColorReportList.size() <= 0) {
            StationReport stationReport = new StationReport();
            stationReport.setAuditing(Constants.AUDIT_TRUE);
            stationReport.setStation(auditReport.getStation());
            stationReport.setCategory(Constants.TECHCOLOR_REPORT);
            stationReport.setReportType(Constants.REPORT_TYPE_COMPLETE);
            stationReport.setUserType(userType);
            stationReport.setUser(auditReport.getUser());
            stationReportRepository.save(stationReport);
            for (AuditColorReport report : auditColorReportList) {
                StationColorReport colorReport = new StationColorReport(report);
                colorReport.setStationReport(stationReport);
                stationColorReportRepository.save(colorReport);
            }
        } else {
            StationReport stationReport = null;
            for (AuditColorReport auditColorReport : auditColorReportList) {
                for (StationColorReport stationColorReport : stationColorReportList) {
                    if (auditColorReport.getQuestionId().equals(stationColorReport.getQuestionId())) {
                        stationReport = stationColorReport.getStationReport();
                        stationColorReport.setAnswerPhoto(auditColorReport.getAnswerPhoto());
                        stationColorReport.setAnswerOther(auditColorReport.getAnswerOther());
                        stationColorReport.setAnswerId(auditColorReport.getAnswerId());
                        stationColorReportRepository.save(stationColorReport);
                    }
                    StationColorReport colorReport = stationColorReportRepository.selectReport(stationReport.getId(), auditColorReport.getQuestionId());
                    if (colorReport == null) {
                        StationColorReport report = new StationColorReport(auditColorReport);
                        report.setStationReport(stationReport);
                        stationColorReportRepository.save(report);
                    }
                }
            }
            stationReport.setAuditing(Constants.AUDIT_TRUE);
            stationReport.setUser(auditReport.getUser());
            Date date = new Date();
            auditReport.setUpdateTime(date);
            auditReport.setUserType(userType);
            auditReport.setUpdateTimeStr(new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT).format(date));
            auditReportRepository.save(auditReport);
            stationReport.setUserType(userType);
            stationReportRepository.save(stationReport);
        }
        updateLogUser(auditReportId, station);
        return JsonResponse.successResponse();
    }

    /**
     * 将station设置进日志
     *
     * @param auditReportId
     * @param station
     */
    private void updateLogUser(Long auditReportId, Station station) {
        AuditColorReport auditColorReport = auditColorReportRepository.selectReport(auditReportId, 175L);
        if (null != auditColorReport && StringUtils.isNotEmpty(auditColorReport.getAnswerOther())) {
            QUserLog qUserLog = QUserLog.userLog;
            BooleanBuilder builder = new BooleanBuilder();
            builder.and(qUserLog.deleted.eq(false));
            builder.and(qUserLog.enabled.eq(true));
            builder.and(qUserLog.station.isNull());
            builder.and(qUserLog.stationName.eq(auditColorReport.getAnswerOther()));
            List<UserLog> userLogList = (List<UserLog>) userLogRepository.findAll(builder);
            for (UserLog userLog : userLogList) {
                userLog.setStation(station);
                userLogRepository.save(userLog);
            }
        }
    }

    @Override
    public JsonResponse auditFalse(User user, Long reportId, String auditNote) {
        AuditReport auditReport = auditReportRepository.findOne(reportId);
        if (auditReport == null) {
            return JsonResponse.errorResponseWithError("抱歉", "报告不存在");
        }
        auditReport.setAuditing(Constants.AUDIT_FALSE);
        auditReport.setAuditNote(auditNote);

        StationReport stationReport = null;
        if (auditReport.getStation() != null && auditReport.getStation().isCheckStation()) {
            QStationReport qStationReport = QStationReport.stationReport;
            BooleanBuilder builder = new BooleanBuilder();
            builder.and(qStationReport.deleted.eq(false));
            builder.and(qStationReport.enabled.eq(true));
            builder.and(qStationReport.station.id.eq(auditReport.getStation().getId()));
            builder.and(qStationReport.category.eq(Constants.TECHCOLOR_REPORT));
            stationReport = stationReportRepository.findOne(builder);
            if (stationReport == null) {
                return JsonResponse.errorResponseWithError("抱歉", "未找到此维修站原有信息");
            }
            stationReport.setAuditing(Constants.AUDIT_TRUE);
        }
        try {
            if (stationReport != null) {
                stationReportRepository.save(stationReport);
            }
            auditReportRepository.save(auditReport);
            AuditReportRecorder auditReportRecorde = new AuditReportRecorder();
            auditReportRecorde.setAuditReport(auditReport);
            auditReportRecorde.setTrue_false(false);
            auditReportRecorde.setUser(user);
            auditReportRecorde.setNote(auditNote);
            auditReportRecorderRepository.save(auditReportRecorde);
            return JsonResponse.successResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResponse.errorResponseWithError("抱歉", "审核失败，请重试");
        }
    }

    private List<Long> getAuditColorReportAnswer(String data, Long questionId) {
        QAuditColorReport qAuditColorReport = QAuditColorReport.auditColorReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qAuditColorReport.questionId.eq(questionId));
        //省份
        if (questionId == 173L || questionId == 174L) {
            if (questionId == 173L) {
                List<String> provinceStrList = new ArrayList<>();
                for (String province : data.split(",")) {
                    provinceStrList.add(province);
                }
                builder.and(qAuditColorReport.answerOther.in(provinceStrList));
            } else {
                builder.and(qAuditColorReport.answerOther.eq(data));
            }
        }
        //维修站类型
        if (questionId == 7L) {
            QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findByQuestionIdAndName(data, 7L);
            if (null != questionAnswerOption) {
                builder.and(qAuditColorReport.answerId.eq(questionAnswerOption.getId().toString()));
            }
        }
        //品牌
        if (questionId == 8L) {
            QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findByQuestionIdAndName(data, 8L);
            if (null != questionAnswerOption) {
                builder.and(qAuditColorReport.answerId.like("%" + questionAnswerOption.getId().toString() + "%"));
            }
        }
        //维修站名称
        if (questionId == 175L) {
            builder.and(qAuditColorReport.answerOther.like("%" + data + "%"));
        }
        List<AuditColorReport> auditColorReportList = (List<AuditColorReport>) auditColorReportRepository.findAll(builder);
        List<Long> auditReportIds = new ArrayList<>();
        for (AuditColorReport auditColorReport : auditColorReportList) {
            if (null != auditColorReport.getAuditReport()) {
                auditReportIds.add(auditColorReport.getAuditReport().getId());
            }
        }
        return auditReportIds;
    }

    @Override
    public JsonResponse getAuditTrueReport(Long startTime, Long endTime, User clientUser,
                                           Boolean isPassed, String provinceStr, String cityVal, String category,
                                           String brandName, String stationName, String userName, Pageable pageable) {
        List<Long> auditReportIdList = new ArrayList<>();
        //省份
        if (StringUtils.isNotEmpty(provinceStr) && StringUtils.isEmpty(cityVal)) {
            List<Long> resultIdList = getAuditColorReportAnswer(provinceStr, 173L);
            if (!resultIdList.isEmpty()) {
                auditReportIdList.addAll(resultIdList);
            }
            //省份+城市:只搜索城市
        } else if (StringUtils.isNotEmpty(cityVal)) {
            List<Long> resultIdList = getAuditColorReportAnswer(cityVal, 174L);
            if (!resultIdList.isEmpty()) {
                auditReportIdList.addAll(resultIdList);
            }
        }
        //维修站类型
        if (StringUtils.isNotEmpty(category)) {
            List<Long> resultIdList = getAuditColorReportAnswer(category, 7L);
            if (!resultIdList.isEmpty()) {
                auditReportIdList.addAll(resultIdList);
            }
        }
        //品牌
        if (StringUtils.isNotEmpty(brandName)) {
            List<Long> resultIdList = getAuditColorReportAnswer(brandName, 8L);
            if (!resultIdList.isEmpty()) {
                auditReportIdList.addAll(resultIdList);
            }
        }
        //维修站名称
        if (StringUtils.isNotEmpty(stationName)) {
            List<Long> resultIdList = getAuditColorReportAnswer(stationName, 175L);
            if (!resultIdList.isEmpty()) {
                auditReportIdList.addAll(resultIdList);
            }
        }
        List<TechColorAuditingReports> reports = new ArrayList<>();
        QAuditReport qStationReport = QAuditReport.auditReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.category.eq(Constants.TECHCOLOR_REPORT));
        builder.and(qStationReport.auditing.ne(Constants.AUDIT_APPLY));
        //技术人员名称
        if (StringUtils.isNotEmpty(userName)) {
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder userBuilder = new BooleanBuilder();
            userBuilder.and(qUserProfile.deleted.eq(false));
            userBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(userBuilder);
            List<Long> userIdList = new ArrayList<>();
            for (UserProfile userProfile : userProfileList) {
                if (null != userProfile.getUser()) {
                    userIdList.add(userProfile.getUser().getId());
                }
            }
            if (!userIdList.isEmpty()) {
                builder.and(qStationReport.user.id.in(userIdList));
            } else {
                return JsonResponse.successResponseWithData(new ArrayList<>());
            }
        }
        if (startTime != null && startTime != 0 && endTime != null && endTime != 0) {
            builder.and(qStationReport.creationTime.after(new Date(startTime)));
            builder.and(qStationReport.creationTime.before(new Date(endTime)));
        }
        if (isPassed != null) {
            if (isPassed) {
                builder.and(qStationReport.auditing.eq(Constants.AUDIT_TRUE));
            } else {
                builder.and(qStationReport.auditing.eq(Constants.AUDIT_FALSE));
            }
        }
        if (!auditReportIdList.isEmpty()) {
            builder.and(qStationReport.id.in(auditReportIdList));
        }
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        List<AuditReport> stationReports = auditReportRepository.findAll(builder, pageable).getContent();
        for (AuditReport report : stationReports) {
            TechColorAuditingReports techColorAuditingReports = new TechColorAuditingReports();
            techColorAuditingReports.setId(report.getId());
            techColorAuditingReports.setAudit(report.getAuditing());
            //获取审核结果的记录
            List<String[]> recorderList = getAuditReportRecorder(report.getId());
            techColorAuditingReports.setRecorderList(recorderList);
//            else {
//                List<String[]> recorderList = getAuditReportRecorder(report.getId(), true);
//                techColorAuditingReports.setRecorderList(recorderList);
//            }
            AuditColorReport province = auditColorReportRepository.selectReport(report.getId(), 173L);
            AuditColorReport city = auditColorReportRepository.selectReport(report.getId(), 174L);
            AuditColorReport stationColorReport = auditColorReportRepository.selectReport(report.getId(), 175L);
            if (stationColorReport != null && province != null && city != null) {
                String procinceStr = province.getAnswerOther();
                String cityStr = city.getAnswerOther();
                if (procinceStr.equals(cityStr)) {
                    techColorAuditingReports.setStationName(procinceStr + stationColorReport.getAnswerOther());
                } else {
                    techColorAuditingReports.setStationName(province.getAnswerOther() + city.getAnswerOther() + stationColorReport.getAnswerOther());
                }
            } else {
                techColorAuditingReports.setStationName("默认的维修站名字");
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT);
            techColorAuditingReports.setDateTime(simpleDateFormat.format(report.getUpdateTime()));
            AuditColorReport colorReport = auditColorReportRepository.selectReport(report.getId(), 7L);
            if (colorReport != null) {
                if (StringUtils.isEmpty(colorReport.getAnswerId())) {
                    techColorAuditingReports.setStationType("");
                } else {
                    QuestionAnswerOption answer = questionAnswerOptionRepository.findOne(Long.valueOf(colorReport.getAnswerId()));
                    if (answer == null) {
                        techColorAuditingReports.setStationType("");
                    } else {
                        techColorAuditingReports.setStationType(answer.getName());
                    }
                }
            } else {
                techColorAuditingReports.setStationType("");
            }

            try {
                UserProfile userProfile = userProfileRepository.findByUserId(report.getUser().getId());
                techColorAuditingReports.setUserName(userProfile.getRealName());
            } catch (Exception e) {
                techColorAuditingReports.setUserName("默认的名字");
            }
            reports.add(techColorAuditingReports);
        }
        return JsonResponse.successResponseWithData(reports);
    }

    /**
     * 获取审核结果的记录
     */
    public List<String[]> getAuditReportRecorder(@ApiParam("报告编号") @RequestParam(value = "reportId") Long reportId) {
        QAuditReportRecorder qAuditReportRecorder = QAuditReportRecorder.auditReportRecorder;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qAuditReportRecorder.deleted.eq(false));
        builder.and(qAuditReportRecorder.enabled.eq(true));
        builder.and(qAuditReportRecorder.auditReport.id.eq(reportId));
        List<AuditReportRecorder> auditReportRecorderList = (List<AuditReportRecorder>) auditReportRecorderRepository.findAll(builder);
        List<String[]> recorderList = new ArrayList<>();
        for (AuditReportRecorder recorder : auditReportRecorderList) {
            String[] str = new String[4];
            UserProfile userProfile = userProfileRepository.findByUserId(recorder.getUser().getId());
            str[0] = userProfile.getRealName();
            str[1] = recorder.getCreationTimeStr();
            if (recorder.isTrue_false() == true) {
                str[2] = "审核通过";
                str[3] = "";
            } else {
                str[2] = "审核不通过";
                if (StringUtils.isNotEmpty(recorder.getNote())) {
//                    note.replaceAll("<(S*?)[^>]*>.*?|<.*? />", "");
                    str[3] = recorder.getNote();
                } else {
                    str[3] = "";
                }
            }
            recorderList.add(str);
        }
        return recorderList;
    }

    /**
     * 获取我的技术颜色报告
     *
     * @param pageable
     * @param userId
     * @param audit
     * @return
     */
    @Override
    public JsonResponse getMyColorReport(Pageable pageable, Long userId, String audit) {
        QAuditReport qAuditReport = QAuditReport.auditReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qAuditReport.deleted.eq(false));
        builder.and(qAuditReport.enabled.eq(true));
        builder.and(qAuditReport.supplement.eq(false));
        builder.and(qAuditReport.user.id.eq(userId));
        builder.and(qAuditReport.auditing.eq(audit));
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<AuditReport> stationReportPage = auditReportRepository.findAll(builder, pageable);
        List<TechColorAuditingReports> responseVo = toResponseVo(stationReportPage.getContent());
        Page<TechColorAuditingReports> page = new PageImpl(responseVo, pageable, stationReportPage.getTotalElements());
        return JsonResponse.successResponseWithPageData(page);
    }

    private List<TechColorAuditingReports> toResponseVo(List<AuditReport> stationReportList) {
        List<TechColorAuditingReports> colorAuditingReportsList = new ArrayList<>();
        for (AuditReport stationReport : stationReportList) {
            TechColorAuditingReports techColorAuditingReports = new TechColorAuditingReports();
            techColorAuditingReports.setId(stationReport.getId());
            techColorAuditingReports.setUserType(stationReport.getUserType());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.CDATE_FORMAT);
            techColorAuditingReports.setDateTime(simpleDateFormat.format(stationReport.getUpdateTime()));
            if (null != stationReport.getStation()) {
                techColorAuditingReports.setStationId(stationReport.getStation().getId());
            }
            if (stationReport.getUser() != null) {
                UserProfile userProfile = userProfileRepository.findByUserId(stationReport.getUser().getId());
                if (userProfile == null) {
                    techColorAuditingReports.setUserName("默认的名字");
                } else {
                    techColorAuditingReports.setUserName(userProfile.getRealName());
                }
            } else {
                techColorAuditingReports.setUserName("默认的名字");
            }
            AuditColorReport province = auditColorReportRepository.selectReport(stationReport.getId(), 173L);
            AuditColorReport city = auditColorReportRepository.selectReport(stationReport.getId(), 174L);
            AuditColorReport stationColorReport = auditColorReportRepository.selectReport(stationReport.getId(), 175L);
            if (stationColorReport != null && province != null && city != null) {
                techColorAuditingReports.setStationName(province.getAnswerOther() + city.getAnswerOther() + stationColorReport.getAnswerOther());
            } else {
                techColorAuditingReports.setStationName("默认的维修站名字");
            }

            if (StringUtils.isNotEmpty(stationReport.getAuditNote())) {
                techColorAuditingReports.setAuditNote(stationReport.getAuditNote());
            } else {
                techColorAuditingReports.setAuditNote("");
            }
            colorAuditingReportsList.add(techColorAuditingReports);
        }
        return colorAuditingReportsList;
    }


}
